//防止页面后退
top.history.pushState(null, null, top.document.URL);
top.addEventListener('popstate', function () {
    history.pushState(null, null, top.document.URL);
});
$(function () {
    $.fn.extend({
        animateCss: function (animationName, func) {
            var animationEnd = 'webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend';
            this.addClass('animated ' + animationName).one(animationEnd, function () {
                var that = $(this);
                that.removeClass('animated ' + animationName);
                if (func) {
                    func(that);
                }
            });
        }
    });

    var $mainIframe = $("#mainIframe");
    if ($mainIframe) {
        $(".leftMenu .list:first").after("<div class='list' id='topMenu'><i class='ion ion-home'></i>主菜单</div>");
        $("#topMenu").click(function () {
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        });
        var $leftMenu = $(".leftMenu .list");
        var active = $('div.list.active').attr('link');
        $mainIframe.attr('src', active);
        $leftMenu.click(function () {
            var link = $(this).attr('link');
            if (link) {
                $leftMenu.removeClass('active');
                $(this).addClass('active');
                $mainIframe.attr('src', link);
            }
        });
    }

    /**
     * 全局ajax拦截
     */
    $.ajaxSetup({
        complete: function (XMLHttpRequest, status) {
            if (XMLHttpRequest.status == 401) {
                layer.msg('登录已失效', {
                    icon: 5
                });
                top.location.href = '/login';
            }
            var data = $BySlin.strToJson(XMLHttpRequest.responseText);
            if (!(data instanceof Error)) {
                if (data.message) {
                    layer.msg(data.message);
                }
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            if (XMLHttpRequest.status == 401) {
                layer.msg('登录已失效', {
                    icon: 5
                });
                top.location.href = '/login';
            }
            if (errorThrown) {
                layer.alert("网络故障，请稍后再试!");
            }
        }
    });
});
(function (window, $) {
    "use strict";
    window.$BySlin = {};
    /**
     * 去除前后空格
     *
     * @param str
     * @returns {*}
     */
    $BySlin.trim = function (str) {
        if (String.prototype.trim) {
            return str == null ? "" : String.prototype.trim.call(str);
        } else {
            return str.replace(/(^\s*)|(\s*$)/g, "");
        }
    };
    /**
     * 去除所有空格
     *
     * @param str
     * @returns {string|void|XML}
     */
    $BySlin.trimAll = function (str) {
        return str.replace(/\s*/g, '');
    };
    /**
     * 判断是否是数组
     *
     * @param obj
     * @returns {boolean}
     */
    $BySlin.isArray = function (obj) {
        if (Array.isArray) {
            return Array.isArray(obj);
        } else {
            return obj instanceof Array;
        }
    };
    /**
     * 判断标签是否为Element
     *
     * @param obj
     * @returns {boolean}
     */
    $BySlin.isElement = function (obj) {
        return !!(obj && obj.nodeType == 1);
    };
    /**
     * jsonToStr
     * @param json object
     * @return {*} string
     */
    $BySlin.jsonToStr = function (json) {
        if (typeof json === 'object') {
            return JSON && JSON.stringify(json);
        } else {
            return json;
        }
    };
    /**
     * strToJson
     * @param str string
     * @return {*} object
     */
    $BySlin.strToJson = function (str) {
        if (typeof str === 'string') {
            try {
                return JSON && JSON.parse(str);
            } catch (e) {
                return e;
            }
        } else {
            return str;
        }
    };
    /**
     * 打开窗口
     * @param title 标题
     * @param url 链接
     */
    $BySlin.open = function (title, url, isTitle) {
    	isTitle = isTitle == undefined ? false : true;
        var modal = layer.open({
            type: 2,
            title: isTitle ? title : isTitle,
            skin: 'layui-layer-lan',
            content: url,
            maxmin: false,
            closeBtn: isTitle ? 1 : 0
        });
        layer.full(modal);
    };
    /**
     * 关闭
     * @param data 数据
     */
    $BySlin.close = function (data) {
        var modals = $('div.layer');
        var currentLayer = $('div.layer:last');
        var index = currentLayer.attr('index');
        if (data) {
            modals.eq(modals.length - 1).trigger('myResult', data);
        }
        modals.eq(modals.length - 1).remove();
        layer.close(index);
    };

    /**
     * 设置Table
     *
     * @param url
     *            请求URL
     * @param id
     *            根节点
     * @param func
     *            控制函数方法
     * @param isCheck
     *            是否开启Icheck或者Iradio
     * @param add
     *            拓展参数
     * @param init
     *            初始化方法
     * @return 初始化对象
     */
    $BySlin.addPage = function (url, id, func, isCheck, add, init) {
        var oldUrl = url;
        var data = {
            page: {
            	list: []
            },
            wlMap:{},
            empMap:{},
            depMap:{},
            softMap:{},
            equipMap:{},
            size: 10,
            Selected: [],
            SelectedId: [],
            con: null
        };
        if (add) {
            for (var i = 0; i < add.length; i++) {
                data[add[i]] = '';
            }
        }
        var vue = new Vue(
            {
                el: id,
                data: data,
                created: init,
                methods: {
                    /**
                     * 获取table
                     */
                    getTarget: function () {
                        return el;
                    },
                    /**
                     * 上一页
                     */
                    prePage: function () {
                        loadData(this.page.current - 1, this.size);
                    },
                    /**
                     * 下一页
                     */
                    nextPage: function () {
                        loadData(this.page.current + 1, this.size);
                    },
                    /**
                     * 第一页
                     */
                    firstPage: function () {
                        loadData(1, this.size);
                    },
                    /**
                     * 最后一页
                     */
                    lastPage: function () {
                        loadData(this.page.pages, this.size);
                    },
                    /**
                     * 设置加载数量
                     *
                     * @param event
                     */
                    reSizePage: function (event) {
                        this.size = $BySlin.trimAll(event.target.innerHTML);
                        this.page.current = 1;
                        loadData(this.page.current, this.size);
                    },
                    /**
                     * 跳转
                     *
                     * @param event
                     */
                    toPage: function (event) {
                        this.page.current = event.target.value;
                        if (this.page.current > this.page.pages) {
                            this.page.current = this.page.pages;
                        }
                        loadData(this.page.current, this.size);
                    },
                    /**
                     * 获取索引数据
                     *
                     * @param index
                     * @returns {*}
                     */
                    getContent: function (index) {
                        return this.page.list[index];
                    },
                    /**
                     * 删除索引数据
                     *
                     * @param index
                     * @returns {*}
                     */
                    delContent: function (index) {
                        this.page.list.splice(index, 1);
                    },
                    /**
                     * 替换一条索引数据
                     *
                     * @param index
                     * @param content
                     * @returns {*}
                     */
                    spliceContent: function (index, content) {
                        this.page.list.splice(index, 1, content);
                    },
                    /**
                     * 替换所有数据
                     *
                     * @param content
                     * @returns {*}
                     */
                    replaceContent: function (content) {
                        this.page.list = content;
                    },
                    /**
                     * 获取所有数据
                     *
                     * @returns {*}
                     */
                    getAllContent: function () {
                        return this.page.list;
                    },
                    /**
                     * 添加数据至行末
                     *
                     * @param content
                     *            内容
                     * @returns {*}
                     */
                    appendContent: function (content) {
                        this.page.list.push(content);
                    },
                    /**
                     * 添加多行
                     *
                     * @param content
                     * @returns {*}
                     */
                    concatContent: function (content) {
                        this.page.list = (this.page.list
                            .concat(content));
                    },
                    /**
                     * 获取选中数据
                     *
                     * @returns {Array}
                     */
                    getSelectedContent: function () {
                        return this.Selected;
                    },
                    /**
                     * 获取选中数据的id
                     *
                     * @returns {Array}
                     */
                    getSelectedId: function () {
                        return this.SelectedId;
                    },
                    /**
                     * 开启多选
                     *
                     * @param event
                     * @constructor
                     */
                    IcheckAll: function (event) {
                        if (isCheck) {
                            var checked = event.target.checked;
                            var all = $(id
                                + " td input[type='checkbox']");
                            if (checked === true) {
                                all.each(function (index, element) {
                                    element.checked = true;
                                });
                                addAllAndDeleteAllSelected(this
                                    .getAllContent(), true);
                            } else {
                                all.each(function (index, element) {
                                    element.checked = false;
                                });
                                addAllAndDeleteAllSelected(this
                                    .getAllContent(), false);
                            }
                        }
                    },
                    /**
                     * 开启选中
                     *
                     * @param index
                     * @param event
                     * @constructor
                     */
                    Icheck: function (index, event) {
                        if (isCheck) {
                            var checked = event.target.checked;
                            var all = $(id + " td input[type='checkbox']");
                            var checkAll = $(id + " th input[type='checkbox']");
                            if (checked === true) {
                                all.each(function (index, element) {
                                    if (!element.checked) {
                                        checkAll.each(function (index, element) {
                                            element.checked = false;
                                        });
                                        return false;
                                    } else {
                                        checkAll.each(function (index, element) {
                                            element.checked = true;
                                        });
                                    }
                                });
                                addAndDeleteSelected(this.getContent(index), true);
                            } else {
                                checkAll.attr('checked', false);
                                addAndDeleteSelected(this.getContent(index), false);
                            }
                        }
                    },
                    /**
                     * 开启单选
                     *
                     * @param index
                     * @constructor
                     */
                    Iradio: function (index) {
                        if (isCheck) {
                            this.Selected[0] = this.getContent(index);
                        }
                    },
                    /**
                     * 设置数据
                     *
                     * @param obj
                     */
                    setPage: function (obj) {
                        this.page = obj;
                    },
                    setWlMap: function(obj){
                        this.wlMap = obj;
                    },
                    setempMap: function(obj){
                        this.empMap = obj;
                    },
                    setdepMap: function(obj){
                        this.depMap = obj;
                    },
                    setsoftMap: function(obj){
                        this.softMap= obj;
                    },
                    setequipMap: function(obj){
                        this.equipMap = obj;
                    },
                    /**
                     * 更改url
                     *
                     * @param obj
                     */
                    setUrl: function (obj) {
                        url = obj;
                        loadData(1, this.size);
                    },
                    /**
                     * 还原初始Url
                     */
                    resetUrl: function () {
                        url = oldUrl;
                        loadData(1, this.size);
                    },
                    /**
                     * 刷新
                     */
                    refreh: function () {
                        this.size = 10;
                        this.delExtData();
                        this.resetCon();
                        this.resetUrl();
                        try {
                            init();
                        } catch (e) {
                        }

                    },
                    init: init,
                    /**
                     * 刷新
                     */
                    currentRefreh: function () {
                        loadData(this.page.current, this.size);
                        try {
                            init();
                        } catch (e) {
                        }
                    },
                    /**
                     * 重置拓展数据
                     */
                    delExtData: function () {
                        if (add) {
                            for (var i = 0; i < add.length; i++) {
                                if (vue[add[i]] instanceof Array) {
                                    vue[add[i]] = [];
                                } else if (vue[add[i]] instanceof String) {
                                    vue[add[i]] = '';
                                }
                            }
                        }
                    },
                    /**
                     * 设置查询条件
                     *
                     * @param obj
                     */
                    setCon: function (obj) {
                        this.con = obj;
                        loadData(1, this.size);
                    },
                    /**
                     * 重置查询条件
                     */
                    resetCon: function () {
                        this.con = null;
                    },
                    custom: func
                }
            });

        /**
         * 选择ALL
         *
         * @param obj
         * @param check
         */
        function addAllAndDeleteAllSelected(obj, check) {
            if (check) {
                for (var i = 0; i < obj.length; i++) {
                    vue.Selected.push(obj[i]);
                    vue.SelectedId.push(obj[i].id);
                }
            } else {
                vue.Selected = [];
                vue.SelectedId = [];
            }
        }

        /**
         * 删除选中指定元素
         *
         * @param obj
         * @param check
         */
        function addAndDeleteSelected(obj, check) {
            for (var i = 0; i < vue.Selected.length; i++) {
                if (vue.Selected[i] === obj) {
                    if (check === false) {
                        vue.Selected.splice(i, 1);
                        vue.SelectedId.splice(i, 1);
                        return;
                    }
                }
            }
            vue.Selected.push(obj);
            vue.SelectedId.push(obj.id);
        }

        /**
         * 加载数据
         *
         * @param pageNo
         * @param pageSize
         */
        function loadData(pageNo, pageSize) {
            if (isCheck) {
                $(id + " input[type='checkbox']").each(
                    function (index, element) {
                        element.checked = false;
                    });
            }
            $(id + ' tr').each(function (index, element) {
                if ($(element).hasClass('TR_DATA_RT')) {
                    $(element).remove();
                }
            });
            var nerUrl;
            if (url.indexOf('?') != -1) {
                nerUrl = url + '&pageNo=' + pageNo + '&pageSize=' + pageSize;
            } else {
                nerUrl = url + '?pageNo=' + pageNo + '&pageSize=' + pageSize;
            }
            if (url) {

                $.getJSON(nerUrl, vue.con, function (data) {
                    data.data.current = pageNo;
                    vue.setPage(data.data);
                    if(data.wlMap!=null){
                        vue.setWlMap(data.wlMap);
                    }
                    if(data.depMap!=null){
                        vue.setdepMap(data.depMap);
                    }

                    if(data.softMap!=null){
                        vue.setsoftMap(data.softMap);
                    }

                    if(data.equipMap!=null){
                        vue.setequipMap(data.equipMap);
                    }
                    if(data.empMap!=null){
                        vue.setempMap(data.empMap);
                    }

                });
            } else {
                var blankPage = {};
                blankPage['current'] = 1;
                blankPage['pageSize'] = 10;
                blankPage['totalElements'] = 0;
                blankPage['list'] = [];
                vue.setPage(blankPage);
            }

            vue.Selected = [];// 清空选中
            vue.SelectedId = [];
        }

        loadData(1, vue.size);
        return vue;
    };
})(window, jQuery);
